//
//  HUD+Data.swift
//  BWHUDKit
//
//  Created by zhuxuhong on 2022/3/21.
//

import UIKit
import BWPopupView


extension HUD {
    public enum HUDType {
        case toast, loading, popup, tips, progress
        
        func isMatched(of vc: UIViewController) -> Bool {
            let isClass = vc.isKind(of: Class)
            switch self {
            case .toast, .popup: return isClass
            case .loading:
                if let vc = vc as? HUDSquareVC,
                    let square = vc.hud as? Square,
                    case .loading = square.type {
                    return true
                }
            case .tips:
                if let vc = vc as? HUDSquareVC,
                    let square = vc.hud as? Square,
                    case .tips = square.type {
                    return true
                }
            case .progress:
                if let vc = vc as? HUDSquareVC,
                    let square = vc.hud as? Square,
                    case .progress = square.type {
                    return true
                }
            }
            return false
        }
        var Class: AnyClass {
            switch self {
            case .toast:
                return HUDToastVC.self
            case .loading, .tips, .progress:
                return HUDSquareVC.self
            case .popup:
                return BWPopupController.self
            }
        }
    }
}

extension HUD {
    public enum Duration {
        public static let short: TimeInterval = 1
        public static let long: TimeInterval = 3
        public static let textFitted: TimeInterval = -1
        public static func custom(_ value: TimeInterval) -> TimeInterval {
            return value
        }
        public static func fit(text: String?) -> TimeInterval {
            guard let text = text else { return short }
            return min(long, max(short, Double(text.count)/20))
        }
    }
    public enum ShowPosition {
        case top, center, bottom
    }
    
    public struct Appearance {
        let tintColor: UIColor
        let backgroundColor: UIColor
        let textFont: UIFont
        let cornerRadius: CGFloat
        
        public static let defaultLight = Appearance(tintColor: .black, backgroundColor: .white, textFont: .system14.medium, cornerRadius: 10)
        
        public static let defaultDark = Appearance(tintColor: .white, backgroundColor: .black.withAlphaComponent(0.85), textFont: .system14.medium, cornerRadius: 10)
    }
}

extension HUD {
    public class Data {
        var icon: UIImage?
        var text: String?
        var customView: UIView?
        var duration: TimeInterval
        let maskColor: UIColor
        var blockable: Bool
        var position: HUD.ShowPosition
        public init(
            icon: UIImage? = nil,
            text: String? = nil,
            customView: UIView? = nil,
            duration: TimeInterval = Duration.short,
            maskColor: UIColor = .clear,
            blockable: Bool = true,
            position: HUD.ShowPosition = .center
        ){
            self.icon = icon
            self.text = text
            self.customView = customView
            self.duration = duration.isFinite ? duration : max(Duration.short, duration)
            self.maskColor = maskColor
            self.blockable = blockable
            self.position = position
        }
    }
}

